package Function;

import VWorldGameStudio.FCBlock;
import VWorldGameStudio.FCItem;
import VWorldGameStudio.FCMapAnimation;
import VWorldGameStudio.FCMonster;
import com.google.gson.Gson;
import utils.FCDataSingles;

import java.io.File;
import java.util.*;

public class FCMapAnimationLibrary {

    public static final String DIR_PATH = "./data/mapAnimation/";
    private HashMap<String, FCMapAnimation> fcMapAnimationHashMap = new HashMap<>();
    private int animationNum = 0;

    public FCMapAnimationLibrary() {
        try {
            animationNum = new File(DIR_PATH).listFiles().length;
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getPreCountNum(){
        return animationNum;
    }

    public void loadAimationData() {
        try {
            File[] f =new File(DIR_PATH).listFiles();
            for(int i=0;i<f.length;i++) {
                FCMapAnimation fcMapAnimation = FCDataSingles.getReader().readMapAnimationData(f[i].getAbsolutePath());
                fcMapAnimationHashMap.put(fcMapAnimation.getAName(), fcMapAnimation);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getMapAnimationSize() {
        return fcMapAnimationHashMap.size();
    }

    public ArrayList<FCMapAnimation> getAllAnimations(){
        ArrayList<FCMapAnimation> list = new ArrayList<>(fcMapAnimationHashMap.size());
        Iterator<Map.Entry<String,FCMapAnimation>> iterator =  fcMapAnimationHashMap.entrySet().iterator();
        while (iterator.hasNext()){
            list.add(iterator.next().getValue());
        }
        return list;
    }

    public void registerMapAni(String name, int level, int pngSizeW, int pngSizeH, ArrayList<String> imgUrls, int vi, int playTIme) {
        FCMapAnimation mapAnimation = new FCMapAnimation();
        ArrayList<String> urls = new ArrayList<>(imgUrls.size());
        for(String u: imgUrls){
            String pngName = u.substring(u.lastIndexOf("/"));
            urls.add("./material/png/mapanimation"+pngName);
        }
        mapAnimation.registerMapAni(name, level, pngSizeW, pngSizeH, urls, vi, playTIme);
        if(fcMapAnimationHashMap.containsKey(name)){
            fcMapAnimationHashMap.remove(name);
        }
        fcMapAnimationHashMap.put(name,mapAnimation);
        FCDataSingles.getWriter().writeMapAnimationData(mapAnimation);
    }

    public FCMapAnimation getMapAnimationByName(String name) {
        if(name == null) return null;
        if(name.equals("null")) return null;
        return fcMapAnimationHashMap.get(name);
    }
}
